package quotation.utils;

/**
 * @author: wangzefeng
 * @date: 2023/2/3
 * 注释
 */

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateUtil {
    private static DateFormat[] dateFormats = new DateFormat[]{new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:S"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), new SimpleDateFormat("yyyy-MM-dd HH:mm"), new SimpleDateFormat("yyyy-MM-dd HH"), new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy-MM")};

    public StringToDateUtil() {
    }

    public static Date convert(String source) {
        if (StringUtils.isBlank(source)) {
            return null;
        } else {
            source = source.trim();

            try {
                int timeLength = source.length();
                Long time = Long.parseLong(source);
                if (timeLength == 10) {
                    time = time * 1000L;
                }

                return new Date(time);
            } catch (Exception var9) {
                Date date = null;
                boolean flag = false;
                DateFormat[] var3 = dateFormats;
                int var4 = var3.length;
                int var5 = 0;

                while(var5 < var4) {
                    DateFormat dateFormat = var3[var5];

                    try {
                        date = dateFormat.parse(source);
                        flag = true;
                        break;
                    } catch (ParseException var8) {
                        ++var5;
                    }
                }

                if (flag) {
                    return date;
                } else {
                    throw new IllegalArgumentException("不能解析日期:" + source);
                }
            }
        }
    }
}

